package com.example;

import android.graphics.Color;
import android.graphics.PorterDuff;
import android.view.MotionEvent;
import android.view.View;

/**
 * On touch listener for handling one finger gestures to move the shape over the screen.
 */
public class CustomViewOnTouchListener implements View.OnTouchListener {
    private final int width;
    private final int height;

    /**
     * Full constructor
     *
     * @param width  the width of the shape.
     * @param height the height of the shape.
     */
    public CustomViewOnTouchListener(final int width, final int height) {
        this.width = width;
        this.height = height;
    }

    /**
     * {@inheritDoc}
     */
    @Override
    public boolean onTouch(final View view, final MotionEvent motionEvent) {
        final TouchableView touchableView = (TouchableView) view;
        final float x = motionEvent.getX() - (width / 2);
        final float y = motionEvent.getY() - (height / 2);
        final float pressure = motionEvent.getPressure();
        touchableView.shapeDrawable.setBounds((int) x, (int) y, (int) x + width, (int) y + height);
        final int r = ((int) (((float) 255) * (pressure * 2.0f)));
        final int g = 255 - r;
        touchableView.shapeDrawable.setColorFilter(Color.argb(touchableView.alpha, r, g, 0), PorterDuff.Mode.MULTIPLY);

        view.invalidate();

        return true;
    }
}
